(function($) {
     
      var imgRotationCache = [];

	$.fn.moveImg = function(params){

		$(this).css('-moz-transform',params.rotate);
		$(this).stop();
		$(this).animate({left: params.leftPos, top: params.topPos} ,
					     {duration: params.duration,
                                    easing: params.easing, 
                                    complete:function(){				 	                  
								$(this).animate({left: params.left, top: params.top}                                                                
                                                 );
                                             }
             });
	}
      $.fn.spread = function(images,rotation,imgNumPerRow){

    	
		var duration = 800;
		imgRotationCache = rotation;

		var imgCoordinates = $(this).arrangeImage(images,imgNumPerRow);
		
		jQuery.each(images, function(index, anImage) {
						                 
			var newLeftPost = imgCoordinates[index].left;
			var newTopPost = imgCoordinates[index].top;

			var randomLeftOffset = Math.floor(Math.random() * 50 + 1);
			var randomTopOffset = Math.floor(Math.random() * 50 + 1);

			
			$(anImage).moveImg({rotate:'rotate(0deg)',
					     	  leftPos:newLeftPost - randomLeftOffset, 
		                      topPos:newTopPost - randomTopOffset, 
	                          duration:duration,
	                          left:newLeftPost,
                              top:newTopPost,
                              easing:'cubicEaseOut'});
			
			duration = duration - 100;    	
			

	 	});

	};

      $.fn.gather = function(images,gatherPos){
    	  
		var duration = 800;

		jQuery.each(images, function(index, value) {
			
			var newLeftPost = gatherPos.leftPos;
			var newTopPost = gatherPos.topPos;
			
			var randomLeftOffset = Math.floor(Math.random() * 10 + 1);
			var randomTopOffset = Math.floor(Math.random() * 10 + 1);

			
			duration = duration - 200;

			$(value).moveImg({rotate:imgRotationCache[index],
					     	 leftPos:newLeftPost + randomLeftOffset, 
		                     topPos:newTopPost + randomTopOffset, 
	                         duration:duration,
	                         left:newLeftPost,
                             top:newTopPost,
					         easing:'linear'});
    			
	 	});
	}
      
     

})(jQuery);
